<?php
	

	class LogHandler{
			
		const FILE = "./Log/log.txt";
		const SessionMessageStorage = "LogHandlerMessages";
		const SessionErrorStorage = "LogHandlerErrors";
		
		//Static arrays to store messages and errors
		public static $messageArray = array();
		public static $errorArray = array();
		
		/**
		 * Public static function to store messages
		 * @param String
		 * @return -
		 */
		public static function logMessage($message){
			if(isset($_SESSION[self::SessionMessageStorage])){
				self::$messageArray = $_SESSION[self::SessionMessageStorage];
			}
			LogHandler::$messageArray[] = $message;
			$_SESSION[self::SessionMessageStorage] = LogHandler::$messageArray;
		}
		
		/**
		 * Public static function to store Errors in array, will allso write to file
		 * @param String
		 * @return - 
		 */
		public static function logError($error){
			if(strlen($error)>1){
				$error = date("Y-m-d H:i:s") . " | " .  $error . "\r\n";
			}
			else{
				$error = $error . "\r\n";
			}
			
			//Check if errors in session
			if(isset($_session[self::SessionErrorStorage])){
				LogHandler::$errorArray = $_SESSION[self::SessionErrorStorage];
			}
			LogHandler::$errorArray[] = $error;
			$_SESSION[self::SessionErrorStorage] = LogHandler::$errorArray;
			
			//Print to file
			LogHandler::writeErrorToFile($error);
		}
		
		/**
		 * Private function to write errormessages to file
		 * @param string errormessage
		 * @return -
		 */
		public static function writeErrorToFile($error){
			
			//Try to open the file
			if(!$handle = fopen(self::FILE, 'a')){
				LogHandler::logError("Cannot open file: " . self::FILE);
				return;
			}
			
			//Try to write to the bottom of our file
			if(fwrite($handle, $error) === false){
				LogHandler::logError("Cannot write to file: " . self::FILE);
				return;
			}
			
			//Close file
			fclose($handle);
		}
		
		public static function getMessages(){
			if(isset($_SESSION[self::SessionMessageStorage])){
				return $_SESSION[self::SessionMessageStorage];
			}
			return array();
		}
		
		public static function getErrors(){
			if(isset($_SESSION[self::SessionErrorStorage])){
				return $_SESSION[self::SessionErrorStorage];
			}
			return array();
		}
	}
	
	/**
	 * LogView Class
	 * Used to visualize messages and errors with html
	 */
	class LogView{
		
		/**
		 * Public function to display errormessages with html
		 * @param -
		 * @return string HTML
		 */	
		public static function showErrors(){
			$errorArray = LogHandler::getErrors();
			$errors = "<h2>Errors:</h2>";
			foreach($errorArray as $key => $error){
				$errors .= "<p style='color:red'>$error</p>";
			}	
			return $errors;
		}
		
		/**
		 * Public function to display all messages (errors and non-errors) with html
		 * @param -
		 * @return string HTML
		 */	
		public static function showAllMessages(){
			
			$returnDiv = "<div class='loggedMessagesDiv'><h2>Logged Messages:</h2>";
			
			$returnDiv .= LogView::showErrors();
			
			$returnDiv .= "<h2>Non errors:</h2>";
			
			$messageArray = LogHandler::getMessages();
			
			foreach($messageArray as $key => $message){
				$returnDiv .= "<p style='color:green'>$message</p>";
			}
				
			$returnDiv .= "
					</div>";
					
			return $returnDiv;
		}
		
		/*
		 * Test Area
		 * Here we test both LogHandler and LogView
		 * @param
		 * @return
		 */
		
		public function logTester(){
			//No messages should be shown
			echo "<h1>No messages should be displayed:</h1>";
			echo $this->showAllMessages();
			
			//Log messages
			LogHandler::logMessage("First Message test");
			LogHandler::logError("First Error test, Something whent wrong");
			
			echo "<h1>One message and one error to display:</h1>";
			echo $this->showAllMessages();
			
			//Log messages
			LogHandler::logMessage("Message test again");
			LogHandler::logError("Error test again");
			
			echo $this->showAllMessages();
			echo $this->showErrors();
		}
	}
	
	
	